package view;

import dao.CityDao;
import dao.KeyDao;
import lombok.SneakyThrows;
import utils.Reptile;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class ReadJobFrame extends JFrame {
    public ReadJobFrame() throws SQLException {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLayout(new FlowLayout());
        this.setTitle("爬取数据");
        this.setBounds(500,350,400,120);
        JLabel addressJLabel = new JLabel("地址码");
        Dimension labelDim = new Dimension(50,30);
        addressJLabel.setPreferredSize(labelDim);
  //     JTextField addressField = new JTextField();
        Dimension textField = new Dimension(125,30);
   //     addressField.setPreferredSize(textField);
        JComboBox cityJCom = new JComboBox();
        cityJCom.setPreferredSize(textField);
        cityJCom.addItem("--请选择--");
        List<String> city = CityDao.checkCity();
        for (String str:city){
            cityJCom.addItem(str);
        }


        JLabel jobJLabel = new JLabel("职业");
        jobJLabel.setPreferredSize(labelDim);
        JComboBox keyCom = new JComboBox();
        keyCom.setPreferredSize(textField);
        keyCom.addItem("--请选择--");
        List<String> key = KeyDao.checkKey();
        for (String str:key){
            keyCom.addItem(str);
        }
        //JTextField jobFiled = new JTextField();
       // jobFiled.setPreferredSize(textField);

        JButton selectButton = new JButton("点击爬取");
        selectButton.setPreferredSize(new Dimension(100,30));

        this.add(addressJLabel);
        this.add(cityJCom);
        this.add(jobJLabel);
        this.add(keyCom);
        this.add(selectButton);


        selectButton.addActionListener(new AbstractAction() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String city = (String) cityJCom.getSelectedItem();
                String job = (String) keyCom.getSelectedItem();
                if (city.equals("--请选择--")||job.equals("--请选择--")){
                    JOptionPane.showMessageDialog(null,"请选择");
                }else {
                    String addressCode = CityDao.checkCityCode(city);
                    System.out.println(addressCode);
                    System.out.println(job);
                    System.out.println("开始爬取");
                    new Thread(){
                        @Override
                        public void run() {
                            try {
                                Reptile.readHtmlData(addressCode,job,1);
                            } catch (IOException ioException) {
                                ioException.printStackTrace();
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            } catch (ClassNotFoundException classNotFoundException) {
                                classNotFoundException.printStackTrace();
                            }
                        }
                    }.start();
                }


            }
        });

    }
}
